/*************************************************************************
	> File Name: Trie_test.cc
	> Author: likang
	> Mail:  
	> Created Time: 2015年04月14日 星期二 19时50分09秒
 ************************************************************************/
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;
#define Max 26

/****************************************************
 * 例如输入a to te tea ted te tedd 那么te.num = 3
 * te.count = 2
 * ************************************************/

typedef struct node
{
    struct node *next[Max];
    int num;    //该结点对应字符串作为前缀出现的次数
	int count;  //该结点对应字符串出现的次数
}Node;
//创建一个新节点
Node *createNew()
{
    Node *p=new Node;
    for(int i=0;i<Max;i++)
    {
        p->next[i]=NULL;
    }
    p->num=0;
	p->count = 0;
    return p;
}
//插入一个字符串
void Insert_str(char str[],Node *head)
{
    int len=strlen(str);
    Node *t,*p=head;
    for(int i=0;i<len;i++)
    {
        int c=str[i]-'a';
		p->num++;
        if(p->next[c]==NULL)
        {

            t=createNew();
            p->next[c]=t;
             p=p->next[c];
        }
        else
        {
            p=p->next[c];
        }
    }
	p->count++;

}
int* Search_str(char str[],Node *head)
{
    Node *p=head;
    int len=strlen(str);
    int count=0;
	int n = 0;
	int *num = (int*) malloc(2*sizeof(int));
    for(int i=0;i<len;i++)
    {
        int c=str[i]-'a';
        if(p->next[c]==NULL)
        {

            cout<<"不存在字符串"<<endl;
            count=0;
            return 0;
        }
        else
        {
            p=p->next[c];
            count=p->count;
			n = p->num;
        }
    }
	num[0] = count;
	num[1] = n;
    return num;
}
int main()
{
    cout<<"nihao"<<endl;
    Node *head=createNew();
    char s[10];
    while(cin>>s,strcmp(s,"quit"))
    {
        Insert_str(s,head);
    }

	while(true)
	{
		cin >> s;
		int *c = Search_str(s,head);
		cout << s << " appears" << c[0] << "times" << endl;
		cout << s << " 作为前缀出现" << c[1] << "times" << endl;
		if (strcmp(s,"quit") == 0)
			break;
	}


    return 0;
}